package com.sdy.fs.provider;

import com.sdy.common.constant.Constants;
import com.sdy.common.model.FileObj;
import com.sdy.common.model.Response;
import com.sdy.common.utils.StringUtil;
import com.sdy.fs.api.FsApi;
import com.sdy.fs.biz.model.MetaFile;
import com.sdy.fs.biz.service.FileService;
import com.sdy.fs.biz.service.MetaFileService;
import com.sdy.mvc.annotation.RemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@RemoteService
public class FsProvider implements FsApi {
    @Value("${fs.domain}")
    private String fsDomain;
    @Autowired
    private FileService fileService;
    @Autowired
    private MetaFileService metaFileService;
    
    @Override
    public Response<String> upload(Byte[] data,
                                   String filename,
                                   String type) {
        FileObj p = this.uploadFile(data, filename, type, null);
        return Response.success(p.getPath());
    }

    @Override
    public FileObj uploadFile(Byte[] data, String filename, String type) {
        return this.uploadFile(data, filename, type, null);
    }

    @Override
    public FileObj uploadFile(Byte[] data, String filename, String type, Integer auth) {
        if (StringUtil.isBlank(type)) {
            type = "common";
        }
        FileObj fo = new FileObj().setFileName(filename);
        String localPath = fileService.saveFile(ArrayUtils.toPrimitive(data), filename, type);
        String fileId = UUID.randomUUID().toString();
        if (auth != null) {
            fo.setPath(fsDomain.concat("/file/get?fileId=").concat(fileId));
        } else {
            fo.setPath(localPath.replace(Constants.UPLOAD_FILE_PATH, fsDomain.concat("/file/origin")));
        }
        Date date = new Date();
        metaFileService.save(new MetaFile()
                .setId(fileId)
                .setType(type)
                .setFileSize(data.length)
                .setFileName(fo.getFileName())
                .setUrl(fo.getPath())
                .setLocalPath(localPath)
                .setCreateTime(date)
        );
        fo.setId(fileId);
        fo.setFileSize(data.length);
        fo.setCreateTime(date);
        return fo;
    }

    @Override
    public FileObj getFileInfo(String fileId) {
        if (StringUtil.isBlank(fileId)) {
            return null;
        }
        MetaFile metaFile = metaFileService.lambdaQuery().eq(MetaFile::getId, fileId).one();
        if (metaFile == null) {
            return null;
        }
        return new FileObj().setId(fileId).setPath(metaFile.getUrl()).setFileName(metaFile.getFileName()).setFileSize(metaFile.getFileSize()).setCreateTime(metaFile.getCreateTime());
    }

    @Override
    public List<FileObj> getFileListInfo(List<String> fileIdList) {
        if (CollectionUtils.isEmpty(fileIdList)) {
            return Collections.emptyList();
        }
        List<MetaFile> flist = metaFileService.lambdaQuery().in(MetaFile::getId, fileIdList).list();
        List<FileObj> result = new ArrayList<>();
        flist.forEach(f -> {
            result.add(new FileObj().setId(f.getId()).setPath(f.getUrl()).setFileName(f.getFileName()).setFileSize(f.getFileSize()).setCreateTime(f.getCreateTime()));
        });
        return result;
    }
}
